package com.cardone.common.action;

import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.context.action.Run3Action;

/**
 * bean属性名称支持
 *
 * @author fand.cardone
 *
 */
public class CopyPropertiesMap2MapForNameAction implements Run3Action<Object, Object, Map<String, Object>> {
	@Override
	public void run(final Object source, final Object target, final Map<String, Object> parameters) {
		if (source == null) {
			return;
		}

		if (target == null) {
			return;
		}

		if (MapUtils.isEmpty(parameters)) {
			return;
		}

		@SuppressWarnings("unchecked")
		final Map<String, Object> sourceMap = (Map<String, Object>) source;

		@SuppressWarnings("unchecked")
		final Map<String, Object> targetMap = (Map<String, Object>) target;

		for (final Entry<String, Object> parameterEntry : parameters.entrySet()) {
			final Object value = MapUtils.getObject(sourceMap, parameterEntry.getKey());

			String targetName = null;

			if (parameterEntry.getValue() instanceof String) {
				targetName = (String) parameterEntry.getValue();
			}

			if (StringUtils.isBlank(targetName)) {
				targetName = parameterEntry.getKey();
			}

			targetMap.put(targetName, value);
		}
	}
}
